home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Ken Long / Polygondrian-c / Polygondrian.c < prev    next >
Encoding:
Text File  |  1994-12-04  |  6.2 KB  |  247 lines  |  [TEXT/MMCC]

  1. //• This is a little color salad tossed together by Kenneth A. Long (me).
  2. //• I wanted to do some polygon action and this seemed like a good way to
  3. //• do it.  Random poly's sure do be fast!  Huh?    :-)
  4.  
  5. //• Enjoy!
  6.  
  7. //• kenlong@netcom.com        10 March 1994.
  8. //•-----------------------------------------------------------------------•//
  9.  
  10. //•  Polygondrian.c •//
  11.  
  12. //• Constance •//
  13. //• Ain't got none.
  14.  
  15. //• Globules •//
  16. Rect            windRect;
  17. OSErr            error;
  18. SysEnvRec        theWorld;
  19. CGrafPtr        savePort;
  20. Rect            r;
  21. PolyHandle        poly;
  22. Pattern            p;
  23. WindowPtr        window;
  24. short            horiz, vert;
  25. Boolean            QDAvail;
  26. RGBColor         rgb;
  27. Boolean            hasColorQD;
  28.  
  29. RgnHandle        mBarRgn, GrayRgn;
  30. short            *mBarHeightPtr;
  31. short            twenty_pixels;
  32.  
  33. //• Prototypes •//
  34. void            Random_Poly_Points (Rect *rectPtr);
  35. unsigned short    The_Ramdominator (unsigned short min, unsigned short max);
  36. void            Draw_The_Poly (void);
  37. void            Do_Init_Managers (void);
  38. void            Hide_Menu_Bar (void);
  39. void            Show_Menu_Bar (void);
  40. Boolean            Check_The_Equipment (void);
  41. void            Set_Up_Window (void);
  42. void            main (void);
  43. int                Main_Event_Loop (void);
  44.  
  45. void Random_Poly_Points (Rect *rectPtr)
  46. {
  47.     unsigned short start_H, start_V;
  48.  
  49.     window = FrontWindow ();
  50.     
  51.     //• Start recording a polygon.
  52.     poly = OpenPoly ();
  53.     
  54.     //• Pick a random hoizontal and vertical point for our starting point.
  55.     start_H = The_Ramdominator (0, qd.screenBits.bounds.right);
  56.     start_V = The_Ramdominator (0, qd.screenBits.bounds.bottom);
  57.     
  58.     //• Move the pen to that location.
  59.     MoveTo (start_H, start_V);
  60.  
  61.     //• Set up 5 random poly bounds lines starting from there.
  62.     LineTo (The_Ramdominator (0, qd.screenBits.bounds.right), 
  63.             The_Ramdominator (0, qd.screenBits.bounds.bottom));
  64.             
  65.     LineTo (The_Ramdominator (0, qd.screenBits.bounds.right), 
  66.             The_Ramdominator (0, qd.screenBits.bounds.bottom));
  67.             
  68.     LineTo (The_Ramdominator (0, qd.screenBits.bounds.right), 
  69.             The_Ramdominator (0, qd.screenBits.bounds.bottom));
  70.             
  71.     LineTo (The_Ramdominator (0, qd.screenBits.bounds.right), 
  72.             The_Ramdominator (0, qd.screenBits.bounds.bottom));
  73.             
  74.     LineTo (The_Ramdominator (0, qd.screenBits.bounds.right), 
  75.             The_Ramdominator (0, qd.screenBits.bounds.bottom));
  76.     
  77.     //• You could have as many successive LineTo's here as you wanted,
  78.     //• within reason.  Also, you could put specific values in, or even get
  79.     //• a control value.  You could get a previous value and add to 
  80.     //• them or subtract from them for a series effect (like ZoomIdle).
  81.     
  82.     //• Finish the polygon by drawing a line back to the starting point.
  83.     LineTo (start_H, start_V);
  84.             
  85.     //• Stop recording.
  86.     ClosePoly ();
  87. }
  88.  
  89. unsigned short The_Ramdominator (unsigned short min, unsigned short max)
  90. {
  91.     unsigned long ranger, tonto;    //• I knew that one would throw ya!
  92.     unsigned short el_randomo;        //• No sign means pos. and neg. values.
  93.     
  94.     //• Get a random number (based on the date and the time at that call).
  95.     el_randomo = Random ();
  96.     
  97.     //• Just to be sure...if it's less than 0 (which it shouldn't be)
  98.     //• then multiply by -1 to make it positive.
  99.     if (el_randomo < 0)
  100.         el_randomo *= -1;
  101.     
  102.     //• The next three lines are a formula for generating a random
  103.     //• number within a specific range.  Don't worry about the 
  104.     //• mathematics, just know where you can look such a formula up 
  105.     //• if you ever need it.
  106.     
  107.     //• The range IS the maximum value minus the minimum value.
  108.     ranger = max - min;
  109.     
  110.     //• These two bytes hold the random number, muliplied by the range
  111.     //• then that product divided by 65,535.
  112.     tonto = (el_randomo * ranger) / 65535;
  113.     
  114.     //• Then, that product is added to minimum value and returned to caller.
  115.     return (tonto + min);   
  116. }
  117.  
  118. //• Set the forecolor with a single line.
  119. SetForeColor (short red, short green,short  blue)
  120. {
  121.     RGBColor theColor;
  122.  
  123.     theColor.red = red;
  124.     theColor.green = green;
  125.     theColor.blue = blue;
  126.     RGBForeColor(&theColor);
  127. }
  128.  
  129. //• Initialize everything for the program, make sure we can run.
  130. void Draw_The_Poly (void)
  131. {
  132.     Rect         poly_wrecked;
  133.     RGBColor    poly_color;
  134.     
  135.     poly_color.red   = Random ();        //• So are there 281,474,976,710,656
  136.     poly_color.green = Random ();        //• possible combinations?
  137.     poly_color.blue  = Random ();        //• Try 2 /*814749767106*/ 56 :)
  138.     
  139.     Random_Poly_Points (&poly_wrecked);    //• Figure the poly points.
  140.     
  141.     RGBForeColor (&poly_color);            //• Snag a random color for the RGB.
  142.     
  143.     //• Color paint it.
  144.     PaintPoly (poly);
  145.  
  146.     //• Say what color the line is.
  147.     SetForeColor (0, 0, 0);
  148.     
  149.     //• Suitable for framing!
  150.     FramePoly (poly);            //• They're uglier with no frame!
  151.     
  152.     //• Bury it.
  153.     KillPoly (poly);
  154.  
  155. }    //• Now, "doitallagainreallyreallyfast!!!" (while not button, remember?).
  156.  
  157. void Do_Init_Managers (void)
  158. {
  159.     MaxApplZone ();
  160.  
  161.     InitGraf (&qd.thePort);
  162.     InitFonts ();
  163.     InitWindows ();
  164.     InitMenus ();
  165.     TEInit ();
  166.     InitDialogs (nil);
  167.     InitCursor ();
  168. }
  169.  
  170. void Hide_Menu_Bar (void) 
  171. {
  172.     Rect    mBarRect;
  173.  
  174.     GrayRgn = GetGrayRgn ();
  175.     mBarHeightPtr = (short *)  0x0BAA;
  176.     twenty_pixels = *mBarHeightPtr;
  177.     *mBarHeightPtr = 0;
  178.     mBarRect = qd.screenBits.bounds;
  179.     mBarRect.bottom = mBarRect.top + twenty_pixels;
  180.     mBarRgn = NewRgn ();
  181.     RectRgn (mBarRgn, &mBarRect);
  182.     UnionRgn (GrayRgn, mBarRgn, GrayRgn);
  183.     PaintOne (0L, mBarRgn);
  184. }
  185.  
  186. void Show_Menu_Bar (void) 
  187. {
  188.     *mBarHeightPtr = twenty_pixels;
  189.     DiffRgn (GrayRgn, mBarRgn, GrayRgn);
  190.     DisposeRgn (mBarRgn);
  191. }
  192.  
  193. Boolean Check_The_Equipment ()
  194. {
  195.     SysEnvRec mySE;
  196.     
  197.     SysEnvirons (2,&mySE);
  198.     
  199.     if (!mySE.hasColorQD)
  200.         ExitToShell ();
  201. }
  202.  
  203. void Set_Up_Window (void)
  204. {
  205.     windRect = qd.screenBits.bounds;
  206.     
  207.     window = NewCWindow (0L, &windRect, "\p", true, plainDBox, (WindowPtr)-1L, false, 0);
  208.     if (window == nil)
  209.     {
  210.         SysBeep (10);
  211.         ExitToShell ();
  212.     }
  213.     //• Say where we'll draw.  Set window to current graf port.
  214.     SetPort (window);
  215.  
  216.     //• Background color.
  217.     FillRect (& (window->portRect), &qd.black);    //• We already know it's portRect!
  218. }
  219.  
  220. void main (void)
  221. {
  222.     long ticks;
  223.  
  224.     Do_Init_Managers ();
  225.  
  226.     Check_The_Equipment ();
  227.     Hide_Menu_Bar ();
  228.     Set_Up_Window ();
  229.  
  230.     //• The seed for the random number generation.
  231.     GetDateTime ( (unsigned long*) &qd.randSeed);
  232.  
  233.     HideCursor ();                //• Don't want that 'fly' buzzin' around.
  234.  
  235.     while (! Button ())            //• While we ain't clickin...
  236.     {
  237.         Draw_The_Poly ();        //• draw to the tickin'.
  238. //•        Delay (60L, &ticks);    //• Put the brakes on, a little...
  239.                                 //• (uncomment for slow).
  240. //•     FillRect (& (window->portRect), black);
  241.                                 //• (uncomment for single).
  242.     }
  243.         ShowCursor ();
  244.     Show_Menu_Bar ();
  245.     ExitToShell ();
  246. }
  247.